﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestProject
{
    public partial class frmCodeGenerator : UIPage
    {
        /// <summary>
        /// 数据库上下文
        /// </summary>
        DbContexts db = new DbContexts();

        public frmCodeGenerator()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmCodeGenerator_Load(object sender, EventArgs e)
        {
            try
            {
                //查找到所有数据库
                string strSql = @"SELECT NAME FROM MASTER..SYSDATABASES ORDER BY DBID";
                var dt = db.GetDataTable(strSql);

                //绑定数据库名称 到ListBox
                this.lst_DbName.DataSource = dt;
                this.lst_DbName.DisplayMember = "NAME";
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 选中的数据库名字发生变化后 联动加载表名称
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lst_DbName_SelectedValueChanged(object sender, EventArgs e)
        {
            try
            {
                //数据库名称
                var dbname = string.Empty;

                //获取选中的数据库名称
                DataRowView dr = lst_DbName.Items[this.lst_DbName.SelectedIndex] as DataRowView;
                for (int i = 0; i < dr.Row.ItemArray.Length; i++)
                {
                    dbname = dr.Row[i].ToString();
                    this.slab_dbName.Text = dbname;
                }

                //查询得到该数据库下所有表
                string strSql = @"USE [" + dbname + @"] SELECT NAME FROM SYSOBJECTS WHERE TYPE='U' ORDER BY ID";
                var dt = db.GetDataTable(strSql);

                //绑定数据库名称 到ListBox
                this.lst_TbName.DataSource = dt;
                this.lst_TbName.DisplayMember = "NAME";
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 选择的表名称发生变化后 联动加载代码预览
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lst_TbName_SelectedValueChanged(object sender, EventArgs e)
        {
            try
            {
                string tbname = string.Empty;

                if (lst_TbName.SelectedItems.Count > 0)
                {
                    DataRowView tbdr = lst_TbName.Items[this.lst_TbName.SelectedIndex] as DataRowView;

                    for (int i = 0; i < tbdr.Row.ItemArray.Length; i++)
                    {
                        tbname = tbdr.Row[i].ToString();
                        this.slab_tbName.Text = tbname;
                    }
                }
            }
            catch (Exception ex)
            {
                ShowWarningDialog(ex.Message);
            }
        }
    }
}
